package com.thuanhoang.boo.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.thuanhoang.boo.R;
import com.thuanhoang.boo.model.ObjGroupCategoryItem;

public class MenuCategoryAdapter extends BaseExpandableListAdapter {
	private ArrayList<ObjGroupCategoryItem> listGroup;
	private Context context;
	/***
	 * @author LinhTH
	 * @param context
	 * @param lstObjMemberDetails
	 */
	public MenuCategoryAdapter(Context context,
			 ArrayList<ObjGroupCategoryItem> listGroup) {
		this.context = context;
		this.listGroup = listGroup;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this.listGroup.get(groupPosition).getListChild()
				.get(childPosititon).getProductMerchCatName();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final String childText = (String) getChild(groupPosition, childPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.menu_item, null);
		}

		TextView txtListChild = (TextView) convertView.findViewById(R.id.productMerchCatName);

		txtListChild.setText(childText);
		return convertView;

	}

	@Override
	public int getChildrenCount(int groupPosition) {
//		if(groupPosition == 0){
//			return 0;
//		}
//		else{
			return listGroup.get(groupPosition).getListChild().size();
//		}

	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.listGroup.get(groupPosition);
	}

	@Override
	public int getGroupCount() {

		return listGroup.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		// infate new view
		LayoutInflater infalInflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
//		if(groupPosition == 0){
//			convertView = infalInflater.inflate(R.layout.menu_item,
//					null);
//			TextView txtListChild = (TextView) convertView.findViewById(R.id.productMerchCatName);
//
//			txtListChild.setText("Tất cả");
//			return convertView;
//		}
//		else{
			convertView = infalInflater.inflate(R.layout.group_item,
					null);
			TextView mTvGroupName = (TextView) convertView
					.findViewById(R.id.grouptitle);
			mTvGroupName.setText(listGroup.get(groupPosition)
					.getGroupItem().getProductMerchCatName());
			
			TextView mTvInfo = (TextView) convertView.findViewById(R.id.groupinfor);
			mTvInfo.setText("...");
			ImageView mIndicator = (ImageView) convertView.findViewById(R.id.show);

			// check if this group is expanded or not
			if (isExpanded) {
				mTvInfo.setVisibility(View.GONE);
				mIndicator.setImageDrawable(context.getResources().getDrawable(
						R.drawable.ic_action_collapse));
			} else {
				mTvInfo.setVisibility(View.VISIBLE);
				mIndicator.setImageDrawable(context.getResources().getDrawable(
						R.drawable.ic_action_expand));
			}
			if(groupPosition==0){
				mTvInfo.setVisibility(View.GONE);
				mIndicator.setVisibility(View.GONE);
			}
			return convertView;
	//	}

	}

	
	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	/**
	 * 
	 */
	public void updateAdapter() {
		this.notifyDataSetChanged();
	}

}
